package fr.phil.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;

import fr.phil.daoservice.Iservices;
import fr.phil.model.beans.Config;

/**
 * @author Philippe <h1>Project X</h1>
 * @19 dec. 2012
 */

// l'implementation de SessionAware permet d'ajouter la session 
// dans la ValueStack de "fr.phil.controller.CoreController"
// qui est visible dans le DEBUG de struts2

public class CoreController extends ActionSupport implements SessionAware {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5426751241760448184L;

	/**
	 * Le logger .
	 */
	private static final Logger LOG = Logger.getLogger(CoreController.class);

	/**
	 * La session injectee par struts.
	 */
	private transient  Map<String, Object> session;

	/**
	 * Session Bean.
	 */
	private Config config;

	/**
	 * liste des objets Config.
	 */
	private List<Config> listeConfig;

	// ============== Service DAO H Q L (hibernate) ===================== //

	/**
	 * The implementation hql for the dao.
	 */
	private Iservices<Config> serviceConfig;

	/**
	 * @param impHqlConfig = .
	 */
	public CoreController(Iservices<Config> serviceConfig) {
		super();
		this.serviceConfig = serviceConfig;
		
	}


	// *************************** Struts methodes ********************** //

	/**
	 * 
	 */
	public CoreController() {
		super();
	}


	/** 
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 * override.
	 * @return SUCCESS.
	 */
	public final String execute() {
		LOG.info("Launch execute method");
		//UtilTimerStack.setActive(true);
		return SUCCESS;
	}

	/**
	 * @return SUCCESS.
	 */
	public final String index() {

		LOG.info("Entering in index <<<-- Boot of application ---->>>");
		addActionMessage("INIT : Struts 2 OK !" + config);
		if (config != null) {
			
			addActionMessage("INIT : Spring 3 OK !");
			

			serviceConfig.saveObject(config);
			
			listeConfig = serviceConfig.listObject();
			
		}
		
		if (listeConfig != null) {
			addActionMessage("INIT : Hibernate 3 OK !");
			LOG.info("Exit in index with SUCCESS result");
		}

		return SUCCESS;
	}

	/**
	 * @return SUCCESS.
	 */
	public final String seConnecter() {
		LOG.info("execution => seConnecter");
		// recuperation de la session 	
		final HttpSession httpSession = 
				ServletActionContext.getRequest().getSession(true);
		httpSession.setAttribute("Connected", true);
		return SUCCESS;
	}

	/**
	 * @return the config
	 */
	public final Config getConfig() {
		return config;
	}

	/**
	 * @param pconfig the config to set
	 */
	public final void setConfig(final Config pconfig) {
		this.config = pconfig;
	}

	/**
	 * @return the listeConfig
	 */
	public final List<Config> getListeConfig() {
		return listeConfig;
	}

	/**
	 * @param plisteConfig the listeConfig to set
	 */
	public final void setListeConfig(final List<Config> plisteConfig) {
		this.listeConfig = plisteConfig;
	}

	@Override
	public final void setSession(final Map<String, Object> psession) {
		this.session = psession;
	}

	/**
	 * @return the session
	 */
	public final Map<String, Object> getSession() {
		return session;
	}


}
